package jlb.andriva.wrappers;

import jlb.andriva.R;
import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Limits {

    private String mLimit;
    private String mCompraUnPago;
    private String mCompraCuotas;
    private String mAdelanto;
    private Element mInfo;

    public Limits(Element root){
        Elements tds = root.select("td[align=left]");
        mLimit = tds.get(0).text().trim();
        mInfo = tds.get(1).child(0);
        mLimit = mLimit.substring(mLimit.indexOf("$"));
        Element tr = root.select("tr[bgcolor=#B6D6F1]").first();
        tds = tr.select("td");
        mCompraUnPago = "$ " + tds.get(1).text().trim();
        mCompraCuotas = "$ " + tds.get(2).text().trim();
        mAdelanto = "$ " + tds.get(3).text().trim();
    }

    public LinearLayout getView(Context context, View.OnClickListener infoHandler){
        LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //mainParams.setMargins(2, 2, 2, 2);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        params.weight = 1;
        params.setMargins(2, 2, 2, 2);

        LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
        params.setMargins(2, 10, 2, 10);

        LinearLayout main = new LinearLayout(context);
        main.setOrientation(LinearLayout.VERTICAL);
        main.setLayoutParams(mainParams);

        LinearLayout mainLim = new LinearLayout(context);
        mainLim.setOrientation(LinearLayout.HORIZONTAL);
        mainLim.setLayoutParams(params);

        LinearLayout mainFirst = new LinearLayout(context);
        mainFirst.setOrientation(LinearLayout.HORIZONTAL);
        mainFirst.setLayoutParams(params);

        LinearLayout mainSecond = new LinearLayout(context);
        mainSecond.setOrientation(LinearLayout.HORIZONTAL);
        mainSecond.setLayoutParams(params);

        LinearLayout mainThird = new LinearLayout(context);
        mainThird.setOrientation(LinearLayout.HORIZONTAL);
        mainThird.setLayoutParams(params);

        LinearLayout mainFourth = new LinearLayout(context);
        mainFourth.setOrientation(LinearLayout.HORIZONTAL);
        mainFourth.setLayoutParams(params);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        LinearLayout.LayoutParams rulerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 2);

        View ruler = inflater.inflate(R.layout.ruler, null);
        ruler.setLayoutParams(rulerParams);
        View ruler2 = inflater.inflate(R.layout.ruler, null);
        ruler2.setLayoutParams(rulerParams);

        main.addView(mainLim);
        main.addView(ruler);
        main.addView(mainFirst);
        main.addView(ruler2);
        main.addView(mainSecond);
        main.addView(mainThird);
        main.addView(mainFourth);

        //LIMITE

        TextView lblLim = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblLim.setLayoutParams(params);
        lblLim.setEllipsize(TextUtils.TruncateAt.END);
        lblLim.setGravity(Gravity.LEFT);
        lblLim.setText(R.string.limits_titleLimit);

        mainLim.addView(lblLim);

        TextView lblLim2 = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblLim2.setLayoutParams(params);
        lblLim2.setEllipsize(TextUtils.TruncateAt.END);
        lblLim2.setTypeface(null, Typeface.BOLD);
        lblLim2.setGravity(Gravity.CENTER);
        lblLim2.setText(mLimit);

        mainLim.addView(lblLim2);

        ImageView imgLim = new ImageView(context);
        imgLim.setLayoutParams(imgParams);
        imgLim.setImageResource(R.drawable.info);
        imgLim.setTag(context.getString(R.string.limits_tagLimit));
        imgLim.setPadding(10,10,10,10);
        imgLim.setOnClickListener(infoHandler);

        mainLim.addView(imgLim);

        //FIRST

        TextView lblDisp = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblDisp.setLayoutParams(params);
        lblDisp.setEllipsize(TextUtils.TruncateAt.END);
        lblDisp.setGravity(Gravity.LEFT);
        lblDisp.setText(R.string.limits_titleDisp);

        mainFirst.addView(lblDisp);

        ImageView imgDisp = new ImageView(context);
        imgDisp.setLayoutParams(imgParams);
        imgDisp.setImageResource(R.drawable.info);
        imgDisp.setTag(context.getString(R.string.limits_tagDisp));
        imgDisp.setPadding(10,10,10,10);
        imgDisp.setOnClickListener(infoHandler);

        mainFirst.addView(imgDisp);

        //SECOND

        TextView lblCompraUnPago = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblCompraUnPago.setLayoutParams(params);
        lblCompraUnPago.setEllipsize(TextUtils.TruncateAt.END);
        lblCompraUnPago.setGravity(Gravity.LEFT);
        lblCompraUnPago.setText(R.string.limits_titleCompraUnPago);

        mainSecond.addView(lblCompraUnPago);

        TextView lblCompraUnPago2 = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblCompraUnPago2.setLayoutParams(params);
        lblCompraUnPago2.setEllipsize(TextUtils.TruncateAt.END);
        lblCompraUnPago2.setTypeface(null, Typeface.BOLD);
        lblCompraUnPago2.setGravity(Gravity.CENTER);
        lblCompraUnPago2.setText(mCompraUnPago);

        mainSecond.addView(lblCompraUnPago2);

        ImageView imgCompraUnPago = new ImageView(context);
        imgCompraUnPago.setLayoutParams(imgParams);
        imgCompraUnPago.setImageResource(R.drawable.info);
        imgCompraUnPago.setTag(context.getString(R.string.limits_tagCompraUnPago));
        imgCompraUnPago.setPadding(10,10,10,10);
        imgCompraUnPago.setOnClickListener(infoHandler);

        mainSecond.addView(imgCompraUnPago);

        //THIRD

        TextView lblCompraCuotas = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblCompraCuotas.setLayoutParams(params);
        lblCompraCuotas.setEllipsize(TextUtils.TruncateAt.END);
        lblCompraCuotas.setGravity(Gravity.LEFT);
        lblCompraCuotas.setText(R.string.limits_titleCompraCuotas);

        mainThird.addView(lblCompraCuotas);

        TextView lblCompraCuotas2 = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblCompraCuotas2.setLayoutParams(params);
        lblCompraCuotas2.setEllipsize(TextUtils.TruncateAt.END);
        lblCompraCuotas2.setTypeface(null, Typeface.BOLD);
        lblCompraCuotas2.setGravity(Gravity.CENTER);
        lblCompraCuotas2.setText(mCompraCuotas);

        mainThird.addView(lblCompraCuotas2);

        ImageView imgCompraCuotas = new ImageView(context);
        imgCompraCuotas.setLayoutParams(imgParams);
        imgCompraCuotas.setImageResource(R.drawable.info);
        imgCompraCuotas.setTag(context.getString(R.string.limits_tagCompraCuotas));
        imgCompraCuotas.setPadding(10,10,10,10);
        imgCompraCuotas.setOnClickListener(infoHandler);

        mainThird.addView(imgCompraCuotas);

        //FOURTH

        TextView lblAdelanto = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblAdelanto.setLayoutParams(params);
        lblAdelanto.setEllipsize(TextUtils.TruncateAt.END);
        lblAdelanto.setGravity(Gravity.LEFT);
        lblAdelanto.setText(R.string.limits_titleAdelanto);

        mainFourth.addView(lblAdelanto);

        TextView lblAdelanto2 = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblAdelanto2.setLayoutParams(params);
        lblAdelanto2.setEllipsize(TextUtils.TruncateAt.END);
        lblAdelanto2.setTypeface(null, Typeface.BOLD);
        lblAdelanto2.setGravity(Gravity.CENTER);
        lblAdelanto2.setText(mAdelanto);

        mainFourth.addView(lblAdelanto2);

        ImageView imgAdelanto = new ImageView(context);
        imgAdelanto.setLayoutParams(imgParams);
        imgAdelanto.setImageResource(R.drawable.info);
        imgAdelanto.setTag(context.getString(R.string.limits_tagAdelanto));
        imgAdelanto.setPadding(10,10,10,10);
        imgAdelanto.setOnClickListener(infoHandler);

        mainFourth.addView(imgAdelanto);

        return main;
    }

    public View getInfo(View img){
        String tag = img.getTag().toString();
        LayoutInflater layoutInflater = (LayoutInflater)img.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.llpopup, null);
        
        TextView msg = (TextView) popupView.findViewById(R.id.popup_msg);
        if(tag.equals(img.getContext().getString(R.string.limits_tagLimit))){
            msg.setText(mInfo.select("td").get(0).text().trim().substring(4));
        }
        if(tag.equals(img.getContext().getString(R.string.limits_tagDisp))){
            msg.setText(mInfo.select("td").get(4).text().trim().substring(4));
        }
        if(tag.equals(img.getContext().getString(R.string.limits_tagCompraUnPago))){
            msg.setText(mInfo.select("td").get(1).text().trim().substring(4));
        }
        if(tag.equals(img.getContext().getString(R.string.limits_tagCompraCuotas))){
            msg.setText(mInfo.select("td").get(2).text().trim().substring(4));
        }
        if(tag.equals(img.getContext().getString(R.string.limits_tagAdelanto))){
            msg.setText(mInfo.select("td").get(3).text().trim().substring(4));
        }
        return popupView;
    }
}
